package com.cade.agriculturemall.view.personalCenter.ordersManage.OrderItems;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.OrderEntity;
import com.cade.agriculturemall.model.ShoppingCarsEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2018/1/6.
 */

public class OrderPresenter extends BasePresenter<OrderContract.IView> implements OrderContract.IPresenter {
    public OrderPresenter(OrderContract.IView view) {
        super(view);
    }

    public int type = 0;

    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    /**
     * 获取订单实体
     *
     * @param dealId  处理单号
     * @param orderId 订单单号
     */
    @Override
    public void downLoadOrderInfo(final String dealId, final String orderId) {
        mView.showProgress();
        AVQuery<OrderEntity> orderAvquery = new AVQuery<>("order");
        if (dealId != null) {
            type = 1;
            orderAvquery.whereEqualTo("deal_no", dealId);
        } else {
            type = 2;
            orderAvquery.whereEqualTo("order_no", orderId);//String 361519980917392
        }
        orderAvquery.include("addressId");
        orderAvquery.findInBackground(new FindCallback<OrderEntity>() {
            @Override
            public void done(List<OrderEntity> list, AVException e) {
                if (e == null) {
                    if (list.size() != 0) {
                        getProList(list);
                    } else {
                        mView.getPro(null, null);
                    }
                } else {
                    e.printStackTrace();
                }
                mView.dismissProgress();
            }
        });
    }

    private List<ShoppingCarsEntity> myShoppingList = new ArrayList<>();
    private AVQuery<ShoppingCarsEntity> query = new AVQuery<>("ShoppingCars");

    //  获取 ShoppingCarsEntity
    public void getProList(final List<OrderEntity> listOrder) {
        mView.showProgress();
        if (listOrder.size() != 0) {
            listOrder.get(0).getDealOrderId();
            if (type == 1) {
                query.whereEqualTo("dealOrderId", listOrder.get(0).getDealOrderId());
            } else if (type == 2) {
                query.whereEqualTo("orderId", listOrder.get(0));
            }
            query.include("pro");
            query.findInBackground(new FindCallback<ShoppingCarsEntity>() {
                @Override
                public void done(List<ShoppingCarsEntity> list, AVException e) {
                    if (e == null) {
                        list.size();
                        if (list.size() != 0) {
                            myShoppingList.addAll(list);
                            mView.getPro(listOrder, myShoppingList);
                        }
                    } else {
                        e.printStackTrace();
                    }
                    mView.dismissProgress();
                }
            });
        }
    }

    @Override
    public void goodsDelete(final OrderEntity orderEntity) {
        String objectId = orderEntity.getObjectId();
        mView.showProgress();
        AVQuery<AVObject> deleteOrder = new AVQuery<>("order");
        deleteOrder.whereEqualTo("objectId", objectId);
        deleteOrder.deleteAllInBackground(new DeleteCallback() {
            @Override
            public void done(AVException e) {
                if (e != null) {
                    mView.showMessage(e.getMessage());
                    mView.deleteFail();
                } else {
                    mView.deleteSuccess();
                }
                mView.dismissProgress();
            }
        });
    }
}
